package com.alstudio.utils.android.geolocation;

import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;

import com.alstudio.utils.android.net.ALWifiManager;
import com.alstudio.utils.log.ALLog;

/**
 * Gps模块管理器
 * 
 * @author alonso lee
 * 
 */
public class ALGpsManager {

	private static ALGpsManager instance;
	private LocationManager mLocationManager;

	private AlGpsLocationListener gpsLocationListener;
	private static Context mContext;

	public ALGpsManager() {
		// TODO Auto-generated constructor stub
		// 获取位置管理器
		mLocationManager = (LocationManager) mContext
				.getSystemService(Context.LOCATION_SERVICE);
	}

	/**
	 * 获取gps管理器
	 * 
	 * @param context
	 * @return
	 */
	public static ALGpsManager getInstance(Context context) {

		if (context == null) {
			throw new IllegalArgumentException("context should not null");
		}

		if (instance == null) {
			mContext = context;
			instance = new ALGpsManager();
		}
		return instance;
	}

	/**
	 * 检测系统GPS模块启动状态
	 * 
	 * @return true 开启 false关闭
	 */
	public boolean isGPSModuleEnable() {

		if (mLocationManager == null) {
			return false;
		}
		if (!(mLocationManager
				.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))) {
			ALLog.e("gps module disable");
			return false;
		}
		return true;
	}

	/**
	 * 获取当前经度
	 * 
	 * @return
	 */
	public String getLat() {
		if (mLocationManager == null) {
			return null;
		}

		// 读取上一个位置
		Location lastLocation = getLastLocation();
		String lat = null;
		if (lastLocation != null) {
			lat = lastLocation.getLatitude() + "";
		}
		return lat;
	}

	/**
	 * 获取当前纬度
	 * 
	 * @return
	 */
	public String getLng() {
		if (mLocationManager == null) {
			return null;
		}

		// 读取上一个位置
		Location lastLocation = getLastLocation();
		String lng = null;
		if (lastLocation != null) {
			lng = lastLocation.getLongitude() + "";
		}
		return lng;
	}

	/**
	 * 获取最近地理位置信息
	 * 
	 * @return
	 */
	public Location getLastLocation() {

		if (mLocationManager == null) {
			return null;
		}

		// 读取上一个位置
		Location lastLocation = null;

		if (isGPSModuleEnable()) {
			lastLocation = mLocationManager
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		} else if (ALWifiManager.isWifiEnable(mContext)) {
			lastLocation = mLocationManager
					.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		} else {
			lastLocation = mLocationManager
					.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
		}
		return lastLocation;
	}

	/**
	 * 启动gps模块
	 */
	public void startGpsModule() {
		Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		try {
			mContext.startActivity(gpsIntent);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * GPS位置变化监听器
	 * 
	 * @param provider
	 * @param listener
	 */
	private void startGpsLocationListener(String provider,
			LocationListener listener) {

		if (mLocationManager == null || provider == null) {
			return;
		}

		Criteria cri = new Criteria();

		cri.setCostAllowed(true);
		// 高精度
		cri.setAccuracy(Criteria.ACCURACY_FINE);
		// 低电量
		cri.setPowerRequirement(Criteria.POWER_LOW);
		// 不要求方位信息
		cri.setBearingRequired(false);
		// 不要求海拔信息
		cri.setAltitudeRequired(false);

		mLocationManager.requestLocationUpdates(provider, 0, 1000, listener);
	}

	private void notifyLocationChanged(Location location) {
		if (gpsLocationListener != null) {
			ALGeoLocationData newLocation = new ALGeoLocationData();

			newLocation.setLat("" + location.getLatitude());
			newLocation.setLng("" + location.getLongitude());

			ALLog.d("gps location changed lat is " + newLocation.getLat()
					+ " \n lng is" + newLocation.getLng());
			gpsLocationListener.onLocationChanged(newLocation);
		}
	}

	/**
	 * 启动gps定位
	 * 
	 * @param listener
	 */
	public void startGPSLocateModule(AlGpsLocationListener listener) {
		startGpsLocationListener(LocationManager.NETWORK_PROVIDER,
				gpsLocateListener);
		startGpsLocationListener(LocationManager.NETWORK_PROVIDER,
				networkLocateListener);
		startGpsLocationListener(LocationManager.NETWORK_PROVIDER,
				passiveLocateListener);

		gpsLocationListener = listener;
	}

	/**
	 * 通知gps定位
	 */
	public void stopGPSLocateModule() {
		removeGPSLocateListener(gpsLocateListener);
		removeGPSLocateListener(networkLocateListener);
		removeGPSLocateListener(passiveLocateListener);

	}

	private void removeGPSLocateListener(LocationListener listener) {
		if (mLocationManager != null) {
			mLocationManager.removeUpdates(listener);
		}
	}

	private ALocationListener gpsLocateListener = new ALocationListener() {

		@Override
		public void onLocationChanged(Location arg0) {
			// TODO Auto-generated method stub
			notifyLocationChanged(arg0);
		}
	};

	private ALocationListener networkLocateListener = new ALocationListener() {

		@Override
		public void onLocationChanged(Location arg0) {
			// TODO Auto-generated method stub
			notifyLocationChanged(arg0);

		}
	};

	private ALocationListener passiveLocateListener = new ALocationListener() {

		@Override
		public void onLocationChanged(Location arg0) {
			// TODO Auto-generated method stub
			notifyLocationChanged(arg0);
		}
	};

	private abstract class ALocationListener implements LocationListener {

		@Override
		public abstract void onLocationChanged(Location arg0);

		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			// TODO Auto-generated method stub

		}
	}

	public interface AlGpsLocationListener extends ALLocationListener {
	}

}
